using System;
using System.Collections.Generic;
using System.Text;
using System.Web.SessionState;
using AppExtensions.Helpers;
using AspNetExtensions.Resources;

namespace AspNetExtensions.Web.SessionState
{
    public class SessionDictionaryWrapper : IDictionary<String, object>
    {
        HttpSessionState state;
        public SessionDictionaryWrapper(HttpSessionState state)
        {
          Guard.ParamIsNotNull(state, "state", ExceptionMessages.HttpSessionStateCannotBeNull);
          this.state = state;
        }

        #region IDictionary<string,object> Members

        public void Add(string key, object value)
        {
            this.state.Add(key, value);
        }

        public bool ContainsKey(string key)
        {
            return this.state[key] != null;
        }

        public ICollection<string> Keys
        {
            get 
            {
                List<string> keys = new List<string>(this.state.Count);
                foreach (string key in this.state.Keys)
                {
                    keys.Add(key);
                }
                return keys;
            }
        }

        public bool Remove(string key)
        {
            if (this.ContainsKey(key))
            {
                this.state.Remove(key);
                return true;
            }
            else
                return false;
        }

        public bool TryGetValue(string key, out object value)
        {
            value = this.state[key];
            return value != null;
        }

        public ICollection<object> Values
        {
            get 
            { 
                List<object> values = new List<object>(this.state.Count); 
                foreach(object value in this.state)
                {
                    values.Add(value);
                }
                return values;
            }
        }

        public object this[string key]
        {
            get
            {
                return this.state[key];
            }
            set
            {
                this.state[key] = value;
            }
        }

        #endregion

        #region ICollection<KeyValuePair<string,object>> Members

        public void Add(KeyValuePair<string, object> item)
        {
            this.Add(item.Key, item.Value);
        }

        public void Clear()
        {
            this.state.Clear();
        }

        public bool Contains(KeyValuePair<string, object> item)
        {
            return this.state[item.Key] != null;
        }

        public void CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public int Count
        {
            get { return this.state.Count; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public bool Remove(KeyValuePair<string, object> item)
        {
            return this.Remove(item.Key);
        }

        #endregion

        #region IEnumerable<KeyValuePair<string,object>> Members

        public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
        {
            foreach (string key in this.state.Keys)
            {
                yield return new KeyValuePair<string, object>(key, this.state[key]);
            }
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.state.GetEnumerator();
        }

        #endregion
    }
}
